package com.lx.huimailf.utils;

import android.os.Handler;
import android.os.Looper;

import com.google.gson.Gson;

import com.lx.huimailf.MyApplication;
import com.zhy.http.okhttp.cookie.store.PersistentCookieStore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Callback;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by QiaoShao on 2016/5/2.
 */
public class OkHttpHelper {
    private static final String TAG="OkHttpHelper";
    private static OkHttpHelper mInstance;
    private Gson gson;
    private Handler mHandler;
    private OkHttpClient mHttpClient;

    static {
        mInstance=new OkHttpHelper();
    }

    public OkHttpHelper(){
        gson=new Gson();
        mHandler = new Handler(Looper.getMainLooper());
        mHttpClient=new OkHttpClient();

        mHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .cookieJar(new CookieJar() {


                    private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();

                    @Override
                    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                        cookieStore.put(url, cookies);


                    }

                    @Override
                    public List<Cookie> loadForRequest(HttpUrl url) {
                        List<Cookie> cookies = cookieStore.get(url);

                        return cookies != null ? cookies : new ArrayList<Cookie>();
                    }
                })
                .build();
    }

    public static  OkHttpHelper getInstance(){
        return  mInstance;
    }

    /**
     * 异步的get请求
     *
     * @param url
     * @param callback
     */
    public void get(String url,Callback callback){

        Request request= buildGetRequest(url);//进一步封装
        getResult(request,callback);

    }

    /**
     * 异步的post请求
     * @param url
     * @param callback
     * @param params
     */
    public void post(String url, Map<String,String> params, Callback callback){

        Request request = buildPostRequest(url,params);
        getResult(request,callback);
    }

    private void getResult(final Request request,final Callback callback){

        mHttpClient.newCall(request).enqueue(callback);
    }


    private  Request buildPostRequest(String url,Map<String,String> params){

        return  buildRequest(url,params);
    }

    private  Request buildGetRequest(String url){

        return  buildRequest(url,null);
    }

    private  Request buildRequest(String url,Map<String,String> params){


        Request.Builder builder = new Request.Builder()
                .url(url);
            RequestBody body = builderFormData(params);
            builder.post(body);

        return builder.build();
    }
    //已修改，能用
    private RequestBody builderFormData(Map<String,String> params){

        FormBody.Builder formBody=null;
        if(params !=null){
            formBody = new FormBody.Builder();
            for (Map.Entry<String,String> entry :params.entrySet() ){
                formBody.add(entry.getKey(),entry.getValue());
            }

        }

        return  formBody.build();

    }


   public enum  HttpMethodType{

        GET,
        POST,

    }
}
